package com.course.api.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.course.api.entity.Comment;
import com.course.api.entity.CommentThumbs;
import com.course.api.mapper.CommentMapper;
import com.course.api.mapper.CommentThumbsMapper;
import com.course.api.service.ICommentThumbsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class CommentThumbsImpl extends ServiceImpl<CommentThumbsMapper, CommentThumbs> implements ICommentThumbsService {

    @Autowired
    CommentThumbsMapper commentThumbsMapper;

    @Autowired
    CommentMapper commentMapper;

    /**
     * 点赞
     * @param commentThumbs
     */
    @Override
    public void addThumbs(CommentThumbs commentThumbs) {
        // 增加评论表点赞数量
        Comment comment = commentMapper.selectById(commentThumbs.getCommentId());
        comment.setThumbsNumber(comment.getThumbsNumber() + 1);
        commentMapper.updateById(comment);

        // 添加评论点赞表信息
        commentThumbsMapper.insert(commentThumbs);
    }

    /**
     * 取消点赞
     * @param id
     */
    @Override
    public void cancelThumbs(Long id) {
        // 减少评论表点赞数量
        CommentThumbs commentThumbs = commentThumbsMapper.selectById(id);
        Comment comment = commentMapper.selectById(commentThumbs.getCommentId());
        comment.setThumbsNumber(comment.getThumbsNumber() - 1);
        commentMapper.updateById(comment);

        // 删除评论点赞表信息
        commentThumbsMapper.deleteById(id);
    }
}
